package com.csxy.lsh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csxy.lsh.entity.EduTeacher;
import com.csxy.lsh.entity.vo.TeacherQuery;
import com.csxy.lsh.mapper.EduTeacherMapper;
import com.csxy.lsh.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper,EduTeacher> implements EduTeacherService {
    @Autowired
    private EduTeacherMapper eduTeacherMapper;

    @Override
    public Page<EduTeacher>  pageListTeacher(long page,long limit){
        Page<EduTeacher> mypage =new Page<>(page,limit);
        Page<EduTeacher> eduTeacherPage = eduTeacherMapper.selectPage(mypage,null);
        return eduTeacherPage;
    }

    @Override
    public void pageQuery(Page<EduTeacher> pageTeacher, TeacherQuery teacherQuery) {
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        if (teacherQuery == null){
            eduTeacherMapper.selectPage(pageTeacher,null);
        }
        if (teacherQuery.getName() != null && !"".equals(teacherQuery.getName())){
            queryWrapper.like("name",teacherQuery.getName());
        }
        if (teacherQuery.getLevel() != null && !"".equals(teacherQuery.getLevel())){
            queryWrapper.eq("level",teacherQuery.getLevel());
        }
        if (teacherQuery.getBegin() != null && !"".equals(teacherQuery.getBegin())){
            queryWrapper.ge("gmt_create",teacherQuery.getBegin());
        }
        if (teacherQuery.getEnd() != null && !"".equals(teacherQuery.getEnd())){
            queryWrapper.le("gmt_modified",teacherQuery.getEnd());
        }
        eduTeacherMapper.selectPage(pageTeacher,queryWrapper);

    }

    @Override
    @Cacheable(key = "'selectHotTeacher'",value = "teacher")
    public List<EduTeacher> selectHotTeacher() {
        QueryWrapper<EduTeacher> Wrapper = new QueryWrapper<>();
        Wrapper.orderByDesc("id");
        Wrapper.last("limit 4");
        return baseMapper.selectList(Wrapper);

    }

    @Override
    public Map<String, Object> getTeacherFrontPageList(Page<EduTeacher> teacherPage) {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");

        baseMapper.selectPage(teacherPage,wrapper);

        HashMap<String, Object> map = new HashMap<>();

        long total = teacherPage.getTotal();

        long size = teacherPage.getSize();

        long current = teacherPage.getCurrent();

        List<EduTeacher> teacherList = teacherPage.getRecords();

        long pages = teacherPage.getPages();

        boolean hasPrevious = teacherPage.hasPrevious();

        boolean hasNext = teacherPage.hasNext();

        map.put("total", total);
        map.put("list", teacherList);
        map.put("size", size);
        map.put("current", current);
        map.put("pages", pages);
        map.put("hasPrevious", hasPrevious);
        map.put("hasNext", hasNext);

        return map;

    }

}
